package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.Constant;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.utils.HttpUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.utils.ReadUrlUtils;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
/**
 * 客户协议控制器
 * 
 * @Date:Created in 2018/7/23 11:28
 * @Author:
 * @Modified By: zjn 2018/7/23 11:28
 */ 
@Controller
@RequestMapping(value = "/security/basic/customer_protocol")
public class CustomerProtocolController extends SecurityController {

    public static final String CUSTOMER_PROTOCOL_URL = "/security/basic_upload/customer_protocol.htm";

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_06_08.getValue());
    }

    @RequestMapping(value = "load_html.htm")
    public void loadHtml(HttpServletResponse response) throws IOException {
        String path = String.format(getAppConfig().staticUrl + "/static/basic/customer_protocol/customer_protocol.html");
        ReadUrlUtils.Result result = ReadUrlUtils.readUrl(path);
        if(result != null && result.httpCode == 200 && StringUtils.isNotEmpty(result.httpContent)) {
            response.setContentType("Content-Type:text/html;charset=UTF-8");
            response.getOutputStream().write(result.httpContent.getBytes("UTF-8"));
        }
    }

    @RequestMapping(value = "update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(String content) throws IOException {
        StringBuilder html = new StringBuilder();
        html.append("<!doctype html><html><head><meta name=\"viewport\" charset='utf-8' content=\"width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no\" /></head><body>");
        html.append(content.replace("<meta charset=\"utf-8\" />", ""));
        html.append("</body></html>");

        File sourceFile = new File(getAppConfig().tempDir, String.format("customer_protocol.html"));
        YhdwUtils.makeParentDir(sourceFile);
        FileUtils.writeStringToFile(sourceFile, html.toString(), Constant.ENCODING_UTF_8);

        HttpUtils.HttpResp httpResp = HttpUtils.uploadFile(getAppConfig().staticUrl + CUSTOMER_PROTOCOL_URL, sourceFile, Collections.EMPTY_MAP); //upload to static server
        if(httpResp.status / 100 == 2) {
            return ExtResult.successResult();
        } else {
            return ExtResult.failResult("保存失败");
        }
    }

}
